package com.guochenglang.www.service.impl;

import com.guochenglang.www.service.CookieService;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Cookie业务
 *
 * @author GuoChenglang
 */
public class CookieServiceImpl implements CookieService {
    /**
     * 添加cookie
     *
     * @param resp  request
     * @param name  键名
     * @param value 值
     */
    @Override
    public void addCookie(HttpServletResponse resp, String name, String value) {
        Cookie cookieUsername = new Cookie(name, value);
        resp.addCookie(cookieUsername);
    }

    /**
     * 删除cookie
     *
     * @param req  request
     * @param resp response
     * @param name 键名
     */
    @Override
    public void deleteCookie(HttpServletRequest req, HttpServletResponse resp, String name) {
        Cookie[] cookies = req.getCookies();
        for (Cookie cookie : cookies) {
            if (name.equals(cookie.getName())) {
                cookie.setMaxAge(0);
                resp.addCookie(cookie);
                break;
            }
        }
    }
}
